package com.kun.index.controller;


import com.kun.common.ResultDto;
import com.kun.index.entity.History;
import com.kun.index.service.impl.HistoryServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 历史记录表 前端控制器
 * </p>
 *
 * @author kunkun
 * @since 2024-03-26
 */
@RestController
@RequestMapping("/index/history")
public class HistoryController {

    @Autowired
    private HistoryServiceImpl historyService;

    @PostMapping("/addOrUpdateHistory")
    @ApiOperation("增加或者更新历史浏览")
    public ResultDto addOrUpdateHistory(@RequestBody History history){
        int num=0;
        if (historyService.selectByHistory(history)!=null){     //更新
            num= historyService.updateHistory(history);
        }else{              //新增
            num= historyService.addHistory(history);
        }
        if (num>0){
            return ResultDto.success("操作成功");
        }else{
            return ResultDto.fail("操作失败");
        }
    }

    @PostMapping("/deleteHistory")
    @ApiOperation("删除历史浏览")
    public ResultDto deleteHistory(@RequestBody History history){
        int num=0;
        num=historyService.deleteHistory(history);
        if (num>0){
            return ResultDto.success("删除历史记录成功");
        }else{
            return ResultDto.fail("删除历史记录失败");
        }
    }

    @GetMapping("/queryHistoryByUserId")
    @ApiOperation("根据用户id查询历史浏览记录")
    public ResultDto queryHistoryByUserId(@RequestParam("pageIndex")Integer pageIndex,
                                          @RequestParam("pageSize")Integer pageSize,
                                          @RequestParam("historyType")String historyType,
                                          @RequestParam("userId")Integer userId){
        HashMap<String, Object> map = new HashMap<>();
        map.put("pageIndex",pageIndex);
        map.put("pageSize",pageSize);
        map.put("historyType",historyType);
        map.put("userId",userId);
        map.put("updateTime_desc","desc");

        Map<String, Object> resultMap = historyService.queryHistoryByConditions(map);
        if (resultMap.get("histories")!=null){
            return ResultDto.success(resultMap);
        }else{
            return ResultDto.fail("查询历史记录失败");
        }
    }

}

